import { EventTouch, Node, UITransform, Vec2, Vec3 } from "cc";
import { RockerCom, TouchEnd, TouchMove, TouchStart } from "./RockerCom";

@et.objectSystem(RockerCom)
class RockerComAwakeSystem extends et.AwakeSystem3<RockerCom, UITransform, UITransform, UITransform>{
    awake(self: RockerCom, rocker: UITransform, rockerCenter: UITransform, touchCom: UITransform): void {
        self.Rocker = rocker;
        self.RockerCenter = rockerCenter;
        self.TouchCom = touchCom;

        self.TouchCom.node.on(Node.EventType.TOUCH_START, self.touchStart, self);
        self.TouchCom.node.on(Node.EventType.TOUCH_MOVE, self.touchMove, self);
        self.TouchCom.node.on(Node.EventType.TOUCH_END, self.touchEnd, self);
    }
}
@et.objectSystem(RockerCom)
class RockerComRegistSystem extends et.RegistSystem<RockerCom>{
    regist(self: RockerCom, isRegist: boolean): void {
        if (isRegist) {
            if (!self.TouchCom) return;
            self.TouchCom.node.off(Node.EventType.TOUCH_START, self.touchStart, self);
            self.TouchCom.node.off(Node.EventType.TOUCH_MOVE, self.touchMove, self);
            self.TouchCom.node.off(Node.EventType.TOUCH_END, self.touchEnd, self);
            self.TouchCom.node.on(Node.EventType.TOUCH_START, self.touchStart, self);
            self.TouchCom.node.on(Node.EventType.TOUCH_MOVE, self.touchMove, self);
            self.TouchCom.node.on(Node.EventType.TOUCH_END, self.touchEnd, self);
        }
        else {
            self.TouchCom.node.off(Node.EventType.TOUCH_START, self.touchStart, self);
            self.TouchCom.node.off(Node.EventType.TOUCH_MOVE, self.touchMove, self);
            self.TouchCom.node.off(Node.EventType.TOUCH_END, self.touchEnd, self);
        }
    }
}
@et.objectSystem(RockerCom)
class RockerComDestroySystem extends et.DestroySystem<RockerCom>{
    destroy(self: RockerCom): void {
        self.Rocker = null;
        self.RockerCenter = null;
        self.TouchCom = null;
    }
}
const tempVec2 = new Vec2();
const tempVec3 = new Vec3();
RockerCom.prototype.touchStart = function (event: EventTouch) {
    let self=this as RockerCom;
    self.Rocker.node.active = true;
    const touchPos = event.getUILocation();
    tempVec3.set(touchPos.x, touchPos.y, 0);
    self.TouchCom.convertToNodeSpaceAR(tempVec3, tempVec3);
    self.Rocker.node.setPosition(tempVec3);
    console.log(tempVec3);
    et.EventSystem.Instance.publish(TouchStart);
}
RockerCom.prototype.touchMove = function (event: EventTouch) {
    let self=this as RockerCom;
    const touchPos = event.getUILocation();
    tempVec3.set(touchPos.x, touchPos.y, 0);
    self.Rocker.convertToNodeSpaceAR(tempVec3, tempVec3);

    let dir = tempVec3.clone().normalize();

    if (tempVec3.lengthSqr() < self.RockerRadius * self.RockerRadius) {
        self.RockerCenter.node.setPosition(tempVec3);
    }
    else {
        let pos = dir.multiply(new Vec3(self.RockerRadius, self.RockerRadius, 0));
        self.RockerCenter.node.setPosition(pos.x, pos.y);
    }
    self.Direction = dir;
    et.EventSystem.Instance.publish(TouchMove, self.Direction);
}
RockerCom.prototype.touchEnd = function (event: EventTouch) {
    let self=this as RockerCom;
    
    self.Rocker.node.active = false;
    et.EventSystem.Instance.publish(TouchEnd);
}
declare module "./RockerCom" {
    interface RockerCom {
        touchStart(event: EventTouch): void;
        touchMove(event: EventTouch): void;
        touchEnd(event: EventTouch): void;
    }
}